View Javadoc

1   /*
2    *  $Id: AbstractDbStatementPool.java,v 1.2 2004/07/25 21:44:43 roku Exp $ 
3    *
4    *  Copyright 2004 University of Hannover
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package olr.statementpool;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import olr.om.Model;
27  import olr.rdf.AbstractStatementPool;
28  import olr.rdf.Attribute;
29  import olr.rdf.Definitions;
30  import olr.rdf.Resource;
31  import olr.rdf.Statement;
32  
33  // TODO Remove passing of modelID since we hold the model as instance var.
34  abstract class AbstractDbStatementPool 
35      extends AbstractStatementPool 
36      implements DbStatementPool
37  {
38      private Map removedDBStatements;
39      private Map importedResources;
40      private Model model;
41      
42      public AbstractDbStatementPool(Model model)
43      {
44          removedDBStatements = new HashMap();
45          importedResources = new HashMap();
46          this.model = model;
47      }
48  
49      public Model getModel()
50      {
51          return this.model;
52      }
53      
54      public void isImportedResource(String subject)
55      {
56          importedResources.put(subject, subject);
57      }
58  
59      /***
60       * Remove a statements from pool and DB.
61       * If the predicate is a course, all statements of the course are also removed.
62       */
63      public void removeAttributeAbout(String subject, Attribute attribute)
64      {        
65          super.removeAttributeAbout(subject, attribute);
66          
67          if(attribute instanceof DBStatement)
68              removedDBStatements.put(attribute, attribute);
69          
70          if(this.isCourse(attribute.getPredicate()))
71          {
72              final List courseAttributes = this.getAttributesAbout(attribute.getObject());
73              
74              for(int i=0; i<courseAttributes.size(); i++)
75                  removeAttributeAbout(attribute.getObject(), (Attribute)courseAttributes.get(i));            
76          }    
77      }
78  
79      public DBStatement saveStatementToDB(Statement statement)
80          throws Exception
81      {
82          return saveAttributeToDB(statement.getSubject(), statement, getModel().getId());
83      }
84  
85      public boolean removeStatementOfUserFromDB(DBStatement statement)
86          throws Exception
87      {
88          return StatementFactory.removeStatement(statement, getModel().getId(), true);
89      }
90  
91      private DBStatement saveAttributeToDB(String subject, Attribute attribute, int modelID)
92          throws Exception
93      {
94          if(attribute instanceof DBStatement)
95          {
96              DBStatement dbStatement = (DBStatement)attribute;
97              if(dbStatement.isModified())
98                  if(StatementFactory.updateStatement(dbStatement))
99                  {
100                     dbStatement.saved();
101                 } else
102                 {
103                     int DBstatementID = StatementFactory.addStatement(subject, dbStatement, modelID);
104                     if(DBstatementID > 0)
105                     {
106                         dbStatement.setDBstatementID(DBstatementID);
107                         dbStatement.saved();
108                     } else
109                     {
110                         throw new Exception("Couldn't add statement to database!");
111                     }
112                 }
113         } else
114         {
115             int DBstatementID = StatementFactory.addStatement(subject, attribute, modelID);
116             if(DBstatementID > 0)
117                 return new DBStatement(subject, attribute, DBstatementID);
118             else
119                 throw new Exception("Couldn't add statement to database!");
120         }
121         return null;
122     }
123 
124     public void saveCourseToDB(String resource)
125         throws Exception
126     {        
127         HashMap savedResources = new HashMap();
128         saveCourseToDB(resource, getModel().getId(), savedResources);
129         for(Iterator it = removedDBStatements.values().iterator(); it.hasNext(); StatementFactory.removeStatement((DBStatement)it.next(), getModel().getId()));
130         removedDBStatements = new HashMap();
131     }
132 
133     private void saveCourseToDB(String resource, int modelID, HashMap savedResources)
134         throws Exception
135     {
136         if(savedResources.containsKey(resource))
137             return;
138         savedResources.put(resource, resource);
139         Resource resourceObj = getResource(resource);
140         if(resourceObj != null)
141         {
142             List attributes = resourceObj.getAttributes();
143             boolean saveThis = resourceObj.getNS().equals(Definitions.getHomeNamespace()) || importedResources.containsKey(resource);
144             if(!saveThis)
145             {
146                 Iterator it = attributes.iterator();
147                 do
148                 {
149                     if(!it.hasNext() || saveThis)
150                         break;
151                     if(it.next() instanceof DBStatement)
152                         saveThis = true;
153                 } while(true);
154             }
155             if(saveThis)
156             {
157                 for(int i = 0; i < attributes.size(); i++)
158                 {
159                     Attribute attribute = (Attribute)attributes.get(i);
160                     DBStatement dbStatement = saveAttributeToDB(resourceObj.getURI(), attribute, modelID);
161                     if(dbStatement != null)
162                         attributes.set(i, dbStatement);
163                     saveCourseToDB(attribute.getObject(), modelID, savedResources);
164                 }
165 
166             }
167         }
168     }
169 }